package com.cdtye.shuoh.controller.mqtt;

import com.cdtye.shuoh.config.mqtt.MqttPushClient;
import com.cdtye.shuoh.config.mqtt.MqttSubClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * MQTT主题订阅，主题消息发布
 * @author ZhongWang Wang
 * @date 2022-12-21
 */
@RestController
@RequestMapping(value = "/mqtt")
public class PublishController {

    @Autowired
    private MqttSubClient mqttSubClient;

    @Autowired
    private MqttPushClient mqttPushClient;


    @RequestMapping(value = "publish")
    public String publish(String topic, String msg) {
        mqttPushClient.publish(topic, msg);
        String data = "发送了一条主题是‘"+topic+"’，内容是:"+msg;
        return data;
    }

    /**
     * 订阅主题
     * @param topic 主题
     * @param qos 消息级别
     * @return
     */
    @RequestMapping(value = "subscribe")
    public String subscribe(String topic, int qos) {
        mqttSubClient.subscribe(topic, qos);
        return "订阅主题'"+topic+"'成功";
    }
}
